<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <title>地图单击拾取经纬度</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <style>
            #container {
                overflow: hidden;
                width: 100%;
                height: 600px;
                margin: 0;
                font-family: "微软雅黑";
            }
            #r-result{width:100%;position: absolute;}
            #r-result input{
                z-index: 999;
                width: auto;
                min-width: 22rem;
                padding: .75rem 1.25rem;
                margin-left: 1.25rem;
                position: fixed;
                top: 1rem;
                background-color: #fff;
                border-radius: .25rem;
                font-size: 14px;
                color: #666;
                border-color: rgba(27, 142, 236, 0.5);
                
            }
            #r-result input:focus{
                border-color: rgba(27, 142, 236, 0.5) ;
                box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
                outline: none;
            }
            .tangram-suggestion-main{
                z-index: 99999;
            }
        </style>
        <script type="text/javascript" src="__SKIN__/js/jquery.js?v={$version}"></script>
        <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak={$editor_bdmap_ak}"></script>
    </head>
    <body style="position: relative;">
        <div id="container"></div>
        <div id="r-result"><input type="text" id="suggestId" size="20" value="" style="width:150px;" placeholder="搜索地点" /></div>
        <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height: auto;display: none;" ></div>
    </body>
</html>

<script type="text/javascript">
    var objMap = new BMapGL.Map('container');
    var storePoint = $("#store_point", parent.document.body).val();
    if ('' == storePoint || !storePoint) {
        var geolocation = new BMapGL.Geolocation();
        geolocation.getCurrentPosition(function(r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var mk = new BMapGL.Marker(r.point);
                objMap.addOverlay(mk);
                objMap.panTo(r.point);
                objMap.centerAndZoom(new BMapGL.Point(r.point.lng, r.point.lat), 15);
                $("#store_point_hidden", parent.document.body).val(r.point.lng+','+r.point.lat);
            } else {
                objMap.centerAndZoom(new BMapGL.Point(116.404, 39.928), 15);
            }
        });
    } else {
        var storePointArr = storePoint.split(',');
        objMap.centerAndZoom(new BMapGL.Point(storePointArr[0],storePointArr[1]), 15);
        var mk = new BMapGL.Marker( new BMapGL.Point(storePointArr[0],storePointArr[1]));
        objMap.addOverlay(mk);
    }

    objMap.enableScrollWheelZoom(true);
    objMap.addEventListener('click', function (e) {
        console.log(e);
        objMap.clearOverlays();
        var mk = new BMapGL.Marker(e.latlng);
        objMap.addOverlay(mk);
        $("#store_point_hidden", parent.document.body).val(e.latlng.lng+','+e.latlng.lat);
    });


    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }

    var ac = new BMapGL.Autocomplete(    //建立一个自动完成的对象
        {"input" : "suggestId"
            ,"location" : objMap
        });

    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

        setPlace();
    });

    function setPlace() {
        objMap.clearOverlays();    //清除地图上所有覆盖物
        function myFun(){
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            objMap.centerAndZoom(pp, 18);
            objMap.addOverlay(new BMapGL.Marker(pp));    //添加标注
            $("#store_point_hidden", parent.document.body).val(pp.lng+','+pp.lat);
        }
        var local = new BMapGL.LocalSearch(objMap, { //智能搜索
            onSearchComplete: myFun
        });
        local.search(myValue);
    }
</script>